﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class NValueBehaviorTypeListEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 480,
                    Height = 360,
                    Child = new UCNValueBehaviorTypeList((List<objst.NValueBehaviorType>)value)
                };
                edSvc.DropDownControl(elHost);
            }
            return value;
        }
    }

    /// <summary>
    /// UCNValueViewTypeList.xaml 的交互逻辑
    /// </summary>
    public partial class UCNValueBehaviorTypeList : UserControl
    {
        List<objst.NValueBehaviorType> _typeList = null;

        public UCNValueBehaviorTypeList(List<objst.NValueBehaviorType> typeList)
        {
            InitializeComponent();

            _typeList = typeList;
            Fill();
        }

        void ck_Checked(object sender, RoutedEventArgs e)
        {
            var ck = (CheckBox)sender;
            var type = (objst.NValueBehaviorType)ck.Tag;
            if (ck.IsChecked == true)
            {
                _typeList.Add(type);
            }
            else
            {
                _typeList.Remove(type);
            }
        }

        void Fill()
        {
            wpList.Children.Clear();
            var names = EnumHelper.GetNames(typeof(objst.NValueBehaviorType));
            foreach (var name in names)
            {
                var type = EnumHelper.Parse<objst.NValueBehaviorType>(name);
                var ck = new CheckBox
                {
                    Tag = type,
                    Focusable = false,
                    Content = new TextBlock { Text = name },
                    IsChecked = _typeList.Contains(type),
                    Margin = new Thickness(0, 0, 4, 4)
                };
                ck.Checked += new RoutedEventHandler(ck_Checked);
                ck.Unchecked += new RoutedEventHandler(ck_Checked);
                wpList.Children.Add(ck);
            }
        }
    }
}
